package monitor

import (
	"html/template"
	"net/http"
	"time"
)
import (
	"buyChance"
	"r_log"
	"util"
)

/*
state概览的页面
*/

type StateOverview struct {
	ExceTime time.Time
	BCBase   []BuychanceBCBase
}

type BuychanceBCBase struct {
	Period  util.Period
	Date    util.DateAndTime
	Time    util.DateAndTime
	Isvalid bool
	Length  int
}

// state页面信息初始化
func (s *StateOverview) init() {
	bcb := &buyChance.BCBase
	s.BCBase = make([]BuychanceBCBase, 4)
	s.ExceTime = time.Now()
	for p := util.PERIOD_DAY; p <= util.PERIOD_HHOUR; p++ {
		bcb.BcLock[p].Lock()
		s.BCBase[p].Period = p
		s.BCBase[p].Date = bcb.BuyChances[p].Date
		s.BCBase[p].Time = bcb.BuyChances[p].Time

		now := time.Now()
		date := util.Today()
		time := util.DateAndTime(now.Hour()*10000 + now.Minute()*100 + now.Second())
		ts := util.GeneratorTimestamp(date, time)

		s.BCBase[p].Isvalid = bcb.BuyChances[p].IsValid(ts, false)
		s.BCBase[p].Length = len(bcb.BuyChances[p].BcSlice)
		bcb.BcLock[p].Unlock()
	}
}

// state 概览信息模板传回
func (s *StateOverview) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	s.init() // 初始化数据
	t := template.Must(template.ParseFiles(monitorunit.MonitorConf.StateAddr))
	err := t.Execute(w, s)
	if err != nil {
		r_log.Logger.Warn("StateInfo template execute is faild: %v", err)
	}
}
